<%@page import="support.*, java.util.*" %>
<%@ page import="java.sql.*"%>
<html>
<head><title>Read Text</title></head>
<body>
<%
	Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
            
	try {
        	Class.forName("org.postgresql.Driver");
                conn = DriverManager.getConnection(
                    "jdbc:postgresql://localhost/admissions?" +
                    "user=postgres&password=27428572");
%>
<%
	support s = new support();
	String pathCountries = config.getServletContext().getRealPath("countries.txt");
	Vector vCountries = s.getCountries(pathCountries); 
	String pathUniversities = config.getServletContext().getRealPath("universities.txt");
	Vector vUniversities = s.getUniversities(pathUniversities); 
	String pathSpecializations = config.getServletContext().getRealPath("specializations.txt");
	Vector vSpecializations = s.getSpecializations(pathSpecializations); 
	String pathMajors = config.getServletContext().getRealPath("majors.txt");
	Vector vMajors = s.getMajors(pathMajors); 


	conn.setAutoCommit(false);
	
	for (int i = 0; i < vCountries.size(); i++) {
		String countries;
		countries = "INSERT INTO countries(name) VALUES ('" + vCountries.get(i) + "')";
		pstmt = conn.prepareStatement(countries);
		pstmt.executeUpdate();
	}
	for (int i = 0; i < vUniversities.size(); i++) {
		Vector p = (Vector) vUniversities.get(i);
		Vector q = (Vector) p.get(1);
		for (int k = 0; k < q.size(); k++) {
			String universities;
			universities = "INSERT INTO universities(name) VALUES ('" + q.get(k) + "')";
			pstmt = conn.prepareStatement(universities);
			pstmt.executeUpdate();
		}	
	}
	for (int i = 0; i < vSpecializations.size(); i++) {
		String specializations;
		specializations = "INSERT INTO specializations(name) VALUES ('" + vSpecializations.get(i) + "')";
		pstmt = conn.prepareStatement(specializations);
		pstmt.executeUpdate();	
	}
		for (int i = 0; i < vMajors.size(); i++) {
		String majors;
		majors = "INSERT INTO disciplines(name) VALUES ('" + vMajors.get(i) + "')";
		pstmt = conn.prepareStatement(majors);
		pstmt.executeUpdate();	
	}
		pstmt = conn.prepareStatement("INSERT INTO disciplines(name) VALUES ('NA')");
		pstmt.executeUpdate();
		
	for (int i = 0; i < vUniversities.size(); i++) {
		Vector universityList = (Vector)vUniversities.get(i);
		String state = (String)universityList.get(0);
		String states;
		states = "INSERT INTO states(name) VALUES ('" + state +"')";
		pstmt = conn.prepareStatement(states);
		pstmt.executeUpdate();
	}
	conn.setAutoCommit(true);
	}

	catch (SQLException e){
		throw new RuntimeException(e);
	}
	finally {
		if(rs != null) {
			try { 
			rs.close();
			} catch (SQLException e){}
			rs = null;
		}
		if(pstmt != null) {
			try {
			pstmt.close();
			} catch(SQLException e) {}
			pstmt = null;
		}
		if(conn != null) {
			try {
			conn.close();
			} catch (SQLException e) {}
			conn = null;
		}
	}
%>
</body>
</html>
